// view/erjilogin/erjilogin.js
let app = getApp()
let yuming = app.globalData.yuming
let reg = app.globalData.mobile
var ying_reg = app.globalData.ying_reg
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '',
    phone: '',
    password: '',
    erpassword: '',
    get_code_btn: true,
    code: '',
    count_down: '60', //秒数
    // xuan: false
    state: '', //0忘记1修改
    yingye: '', //营业执照
    mobile: '', //手机号
    count_down: '60', //秒数


  },
  inputShoweds() {
    this.setData({
      ischus: true
    })
  },
  yingye(e) {
    this.setData({
      yingye: e.detail.value
    })
  },
  bindf() {
    console.log(2111111111111)
    // this.huoyingphone()
  },
  huoyingphone() {
    var that = this
    wx.request({
      url: 'https://msion.86wky.com/shop/shop_creditcode_getbackphone',
      data: {
        creditcode: that.data.yingye,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Token: wx.getStorageSync('token'),
      },
      method: 'post',
      success: (res) => {
        console.log(res)
        that.setData({
          phone: res.data.data
        })
        if (res.data.code != 200) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration:3000
          })
        }
      },
      fail: (error) => {
        reject(error);
      }
    })
  },
  // 验证码
  code(e) {
    this.setData({
      code: e.detail.value
    })
  },
  // 点击获取验证码
  huoqu() {
    var that = this
    if (that.data.phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
    } else if (that.data.phone != '') {
      if (reg.test(that.data.phone) == false) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: "none"
        })
      } else {
        that.yzm()
      }
    }
  },
  // 获取验证码接口
  yzm() {
    var that = this
    wx.request({
      url: 'https://msion.86wky.com/Besall/mobileCode',
      data: {
        mobile: that.data.phone,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Token: wx.getStorageSync('token'),
      },
      method: 'post',
      success: (res) => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        that.setData({
          get_code_btn: false
        })
        that.get_code_settime()
      },
      fail: (error) => {
        reject(error);
      }
    })
  },
  // 倒计时
  get_code_settime: function (that) {
    var that = this;
    var countDown = that.data.count_down; //获取倒计时初始值
    //如果定时器设置在外边，那么用户就看不到countDown的数值变化，所以要把定时器存进data里边
    that.setData({
      //这里把setInterval复制给变量名timer
      timer: setInterval(function () {
        countDown--;
        that.setData({
          get_code_btn: false, //此处用于隐藏验证码点击按钮，使倒计时显示
          count_down: countDown //倒计时计数 
        })
        //当计数为0时，清空定时器，使验证码按钮再次显示
        if (countDown == 0) {
          clearInterval(that.data.timer); //此处我清除timer之后，count_down没有变回我原先设定的120，get_code_btn也没有变回默认的true，咱没搞明白，所以我又重新赋值了
          that.setData({
            get_code_btn: true,
            count_down: '60'
          })
        }
      }, 1000)
    })
  },
  password(e) {
    this.setData({
      password: e.detail.value
    })
  },
  erpassword(e) {
    this.setData({
      erpassword: e.detail.value
    })
  },
  phone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 开始
  inputShowed() {
    this.setData({
      ischu: true
    })
  },
  bindblurs() {
    this.setData({
      ischu: false
    })
  },
  mobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  // 点击获取验证码
  huoqu() {
    var that = this
    if (that.data.mobile == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
    } else if (that.data.mobile != '') {
      if (reg.test(that.data.mobile) == false) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: "none"
        })
      } else {
        that.yzm()
      }
    }
  },
   // 获取验证码接口
   yzm() {
    var that = this
    wx.request({
      url: 'https://msion.86wky.com/Besall/mobileCode',
      data: {
        mobile: that.data.mobile,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Token: wx.getStorageSync('token'),
      },
      method: 'post',
      success: (res) => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        that.setData({
          get_code_btn: false
        })
        that.get_code_settime()
      },
      fail: (error) => {
        reject(error);
      }
    })
  },
  tishi() {
    wx.showToast({
      title: '还有' + this.data.count_down + "秒才能再次获取",
      icon: 'none'
    })
  },
  inputShoweds() {
    this.setData({
      ischus: true
    })

  },
  bindblurss() {
    this.setData({
      ischus: false
    })
  },
  code(e) {
    this.setData({
      code: e.detail.value
    })
  },
  get_code_settime: function (that) {
    var that = this;
    var countDown = that.data.count_down; //获取倒计时初始值
    //如果定时器设置在外边，那么用户就看不到countDown的数值变化，所以要把定时器存进data里边
    that.setData({
      //这里把setInterval复制给变量名timer
      timer: setInterval(function () {
        countDown--;
        that.setData({
          get_code_btn: false, //此处用于隐藏验证码点击按钮，使倒计时显示
          count_down: countDown //倒计时计数 
        })
        //当计数为0时，清空定时器，使验证码按钮再次显示
        if (countDown == 0) {
          clearInterval(that.data.timer); //此处我清除timer之后，count_down没有变回我原先设定的120，get_code_btn也没有变回默认的true，咱没搞明白，所以我又重新赋值了
          that.setData({
            get_code_btn: true,
            count_down: '60'
          })
        }
      }, 1000)
    })
  },
  mobilexiugai(){
    var that = this

    wx.request({
      url: 'https://msion.86wky.com/shop/shopSaveMobile',
      data: {
        mobile: that.data.mobile,
        code:that.data.code,
        shopId:wx.getStorageSync('shopId'),
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Token: wx.getStorageSync('token'),
      },
      method: 'post',
      success: (res) => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1
          });

        },800)
      },
      fail: (error) => {
        reject(error);
      }
    })
  },
  // 结束
  // 勾选协议
  // xuanz() {
  //   if (this.data.xuan == true) {
  //     this.setData({
  //       xuan: false
  //     })
  //   } else {
  //     this.setData({
  //       xuan: true
  //     })
  //   }
  // },
  // 忘记密码
  queding() {
    if (this.data.yingye == '') {
      wx.showToast({
        title: '请输入营业执照',
        icon: 'none'
      })
    }
    // else if (ying_reg.test(this.data.yingye) == false) {
    //   wx.showToast({
    //     title: '营业执照格式错误',
    //     icon: 'none'
    //   })
    // } 
    // else if (this.data.phone == '') {
    //   wx.showToast({
    //     title: '请输入手机号',
    //     icon: 'none'
    //   })
    // } else if (this.data.code == '') {
    //   wx.showToast({
    //     title: '请输入验证码',
    //     icon: 'none'
    //   })
    // }
     else if (this.data.password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
    } else if (this.data.erpassword == '') {
      wx.showToast({
        title: '请再次输入密码',
        icon: 'none'
      })
    } else {
      var that = this
      this.myRequest(yuming + 'shop/shop_creditcode_forgetpass', {
        socialCode: that.data.yingye,
        // mobile: that.data.phone,
        // code: that.data.code,
        password: that.data.password,
        password_plaintext: that.data.erpassword,
      }, 'POST').then(res => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          setTimeout(function () {
            wx.navigateBack({
              delta: 0,
            })
          }, 1000)
        }
      })
    }
  },
  // 修改密码
  deng() {
    if (this.data.phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
    } else if (this.data.code == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
    } else if (this.data.password == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
    } else if (this.data.erpassword == '') {
      wx.showToast({
        title: '请再次输入密码',
        icon: 'none'
      })
    } else {
      var that = this
      this.myRequest(yuming + 'shop/shop_creditcode_savepass', {
        shopId: wx.getStorageSync('shopId'),
        mobile: that.data.phone,
        code: that.data.code,
        password: that.data.password,
        password_plaintext: that.data.erpassword,
      }, 'POST').then(res => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          setTimeout(function () {
            wx.navigateBack({
              delta: 0,
            })
          }, 1000)
        }
      })
    }
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      state: options.state
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})